﻿/**********************************
   * add by zengtongchun 2015-06-11 统一onvaluechanged事件
   **************************************/
var enterExtended = [];
function commentValueChanged(name) {
    for (var i = 0; i < enterExtended.length; i++) {
        if (enterExtended[i].name == name) {
            enterExtended[i].func();
        }
    }
}


/**********************************
   * add by 詹勇 2015-11-15 回车控制 光标跳转事件
   **************************************/
function EnterControlTheCursorJumps(enterorder) {
    //数组第一个获得 焦点
    var firstcontrol = Enumerable.From(enterorder).First();
    mini.get(firstcontrol.name).focus();

    //循环添加 回车事件 的响应
    Enumerable.From(enterorder).ForEach(function (x, index) {
        var object = mini.get(x.name);
        if (object == null) {
            return;
        }
        var className = object.el.className;
        //保证  onenter 事件在同一个控件上不添加两遍
        if (object.o10olo.enter != undefined)
            return;

        if (className == "mini-textbox") {
            mini.get(x.name).set({
                visible: true,
                onenter: function (e) {
 

                    if (index + 1 < enterorder.length) {
                        var lastname = enterorder[index + 1].name;
                        var next;
                        if (x.functionname != null && isFunc(x.functionname)) {

                            next = x.functionname();
                        }
                        if (mini.get(lastname).enabled == false) {
                            lastname = NextEnabled(enterorder, lastname, index + 1);
                        }

                        mini.get(lastname).focus();
                        //mini.get(lastname).focus();

                    }
                    else if (index + 1 == enterorder.length) {
                        //最后一项有 事件就 需要执行
                        x.functionname();
                    }

                }
            });
        } else {
            mini.get(x.name).set({
                visible: true,
                onvaluechanged: function (e) {
                    if (index + 1 < enterorder.length) {
                        var lastname = enterorder[index + 1].name;
                        var next;
                        if (x.functionname != null && isFunc(x.functionname)) {
                            next = x.functionname();
                        }
                        if (mini.get(lastname).enabled == false) {
                            lastname = NextEnabled(enterorder, lastname, index + 1);
                        }
                        mini.get(lastname).focus();
                        //mini.get(lastname).focus();

                    }

                },
                onenter: function (e) {
                    if (index + 1 < enterorder.length) {
                        var lastname = enterorder[index + 1].name;
                        var next;
                        if (x.functionname != null && isFunc(x.functionname)) {
                            next = x.functionname();
                        }
                        if (mini.get(lastname).enabled == false) {
                            lastname = NextEnabled(enterorder, lastname, index + 1);
                        }
                        mini.get(lastname).focus();
                        //mini.get(lastname).focus();

                    }

                }
            });
        }

    });
}

//function ShiftTabBack(enterorder) {
//    Enumerable.From(enterorder).ForEach(function (x, index) {
//        var object = mini.get(x.name);
//        if (object == null) {
//            return;
//        }
//        var className = object.el.className;
//        //保证  onenter 事件在同一个控件上不添加两遍
//        if (object.o10olo.enter != undefined)
//            return;

//        if (className == "mini-textbox") {
//            mini.get(x.name).set({
//                visible: true,
//                onkeydown: function (e) {
//                    alert(e.e.htmlEvent.keyCode);
//                }
//            });
//        }
//    });
//}

//add by zengtongchun 判断下一个控件是否是enabled的
function NextEnabled(obj, NextId, index) {
    var id = NextId;
    var nextObj = mini.get(NextId);
    if (nextObj.enabled == false) {
        id = NextEnabled(obj, obj[index + 1].name, index + 1);
    }
    return id;
}

///判断对象是否是函数
function isFunc(test) {
    return typeof test == 'function';
}